/**
 * less2css
 */

var path = require('path');
var fs = require('fs');
var exec = require('child_process').exec;

module.exports.merger = function(input_file, output_file) {
    var lessc = function(rootPath, targetPath) {
        //取得当前绝对路径
        rootPath = path.resolve(rootPath);
        //目标路径绝对路径
        targetPath = path.resolve(targetPath);
        //判断目录是否存在
        fs.exists(rootPath, function(exists) {
            //路径存在
            if (exists) {
                //获取当前路径下的所有文件和路径名
                var childArray = fs.readdirSync(rootPath);
                if (childArray.length) {
                    for (var i = 0; i < childArray.length; i++) {
                        var currentFilePath = path.resolve(rootPath, childArray[i]);
                        var currentTargetPath = path.resolve(targetPath, childArray[i]);
                        //读取文件信息
                        var stats = fs.statSync(currentFilePath);
                        //若是目录则暂时为include引用目录，不需要递归调用单独编译
                        if (stats.isDirectory()) {
                            //lessc(currentFilePath, currentTargetPath);
                        } else {
                            //判断文件是否为less文件
                            if (path.extname(currentFilePath) === ".less") {
                                var newFilePath = path.resolve(targetPath, path.basename(currentFilePath, '.less') + ".css");
                                if (!fs.existsSync(targetPath)) {
                                    fs.mkdirSync(targetPath);
                                }
                                console.log("succ " + newFilePath);
                                exec("lessc -x " + currentFilePath + " > " + newFilePath);
                            }
                        }
                    }
                }
            } else {
                console.log("directory is not exists");
            }
        });
    }

    lessc(input_file, output_file);
};
